home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / MakeWrite / MakeWrite Folder / MWCheckMark.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-16  |  4.5 KB  |  205 lines  |  [TEXT/KAHL]

  1. # include    "TransSkel.h"
  2.  
  3. # include    "MWMaca.h"
  4. # include    "MakeWrite.h"
  5.  
  6.  
  7. /*
  8.  * markRes holds the result, for each map line, of the current match
  9.  * state.  Element i is true if mark i still matches the input, false
  10.  * if not.
  11.  *
  12.  * markRes is big enough to hold one entry for each formatting
  13.  * specification, plus one entry for each special marker.  The special
  14.  * marks must generally be special-cased.
  15.  *
  16.  * Right now there's two special marks - the paragraph indicator
  17.  * mark and the page break.
  18.  *
  19.  * markPos is the position in the marks that the current input char is
  20.  * matched against.
  21.  */
  22.  
  23. # define    maxMarkers    (maxMappings + extraMarks)
  24.  
  25.  
  26. static Boolean        markRes[maxMarkers];
  27. static short        markPos;
  28.  
  29.  
  30. /*
  31.  * Look for marks that are empty.  This is done before any text->write
  32.  * conversion because empty marks are illegal.
  33.  *
  34.  * Return false if no empty marks, otherwise true.
  35.  *
  36.  * No check on the paragraph marker:  that's handled by the paragraph
  37.  * style dialog.
  38.  */
  39.  
  40. Boolean
  41. FindEmptyMark (void)
  42. {
  43. short    i;
  44.  
  45.     for (i = 0; i < mapList->nLines; ++i)
  46.     {
  47.         if ((*mapSpec[i].mark)[0] == 0)
  48.         {
  49.             SelectMapping (i);
  50.             Message1 ("\pCan't have empty format markers");
  51.             return (true);
  52.         }
  53.     }
  54.     return (false);
  55. }
  56.  
  57.  
  58. /*
  59.  * Initialize all the used mark states (every mark eligible) and
  60.  * reset the mark position
  61.  */
  62.  
  63. void
  64. InitMarkStates (void)
  65. {
  66. short    i;
  67.  
  68.     markPos = 0;
  69.     for (i = 0; i < mapList->nLines; ++i)
  70.         markRes[i] = true;
  71.     for (i = maxMappings; i < maxMarkers; ++i)
  72.         markRes[i] = true;
  73. }
  74.  
  75.  
  76. /*
  77.  * Check current input character against the current mark position
  78.  * of each of the marks that are still in the running for a match.
  79.  * Eliminate those that don't match the char.  For those that do,
  80.  * if the entire mark has been matched, then it's a hit, and the format
  81.  * of the input should be changed to that of the format corresponding
  82.  * to the mark.
  83.  *
  84.  * Return -2 if no more marks are in the running, -1 if any marks
  85.  * are still in the running but none have been matched completely,
  86.  * otherwise return the index of the completely matched mark.
  87.  *
  88.  * Check format markers first, then paragraph marker (if one is being
  89.  * used).
  90.  */
  91.  
  92. short
  93. CheckMarkers (char c)
  94. {
  95. short            i;
  96. short            count = 0;    /* number of matches */
  97. StringHandle    hStr;
  98.  
  99.     ++markPos;                        /* advance to next marker position */
  100.     for (i = 0; i < mapList->nLines; ++i)
  101.     {
  102.         if (markRes[i])                /* if mark still in running */
  103.         {
  104.             hStr = mapSpec[i].mark;
  105.             if (c != (*hStr)[markPos])
  106.                 markRes[i] = false;    /* this one's eliminated now */
  107.             else
  108.             {                        /* this one still matches -- */
  109.                 ++count;            /* has the end been reached? */
  110.                 if (markPos == (*hStr)[0])
  111.                     return (i);        /* yes */
  112.             }
  113.         }
  114.     }
  115.  
  116.     /*
  117.      * Check special marks:  paragraph and page break
  118.      */
  119.     if (paraMark[0] > 0 && markRes[paraMarkIdx])
  120.     {
  121.          if (c != paraMark[markPos])
  122.              markRes[paraMarkIdx] = false;
  123.          else
  124.         {
  125.             ++count;
  126.             if (markPos == paraMark[0])
  127.                 return (paraMarkIdx);
  128.         }
  129.     }
  130.  
  131.     if (pageMark[0] > 0 && markRes[pageMarkIdx])
  132.     {
  133.          if (c != pageMark[markPos])
  134.              markRes[pageMarkIdx] = false;
  135.          else
  136.         {
  137.             ++count;
  138.             if (markPos == paraMark[0])
  139.                 return (pageMarkIdx);
  140.         }
  141.     }
  142.  
  143.     return (count > 0 ? -1 : -2);
  144. }
  145.  
  146.  
  147. /*
  148.  * Check two markers.  If one is a prefix of the other, put up a
  149.  * warning.  Return false if the user says not to continue.  If the
  150.  * user says to continue, or there's no conflict, return true.
  151.  */
  152.  
  153. static Boolean
  154. ConflictCheck (StringPtr s1, StringPtr s2,
  155.                 StringPtr type1, StringPtr type2)
  156. {
  157. short    i, len;
  158.  
  159.     len = s1[0];
  160.     if (s2[0] < len)
  161.         len = s2[0];
  162.     for (i = 1; i <= len; ++i)
  163.     {
  164.         if (s1[i] != s2[i])
  165.             return (true);        /* not a prefix - continue */
  166.     }
  167.     ParamText (type1, s1, type2, s2);
  168.     i = SkelAlert (conflictAlrtNum, SkelDlogFilter (nil, true),
  169.                                         skelPositionOnParentWindow);
  170.     SkelRmveDlogFilter ();
  171.     return (i == 1);
  172. }
  173.  
  174.  
  175. /*
  176.  * Find conflicts among the set of markers
  177.  */
  178.  
  179. void
  180. FindConflicts (void)
  181. {
  182. short            i, j;
  183. StringHandle    h1, h2;
  184. Boolean        loop = true;
  185.  
  186.     for (i = 0; loop && i < mapList->nLines; ++i)
  187.     {
  188.         h1 = mapSpec[i].mark;
  189.         HLock ((Handle) h1);
  190.         for (j = i + 1; loop && j < mapList->nLines; ++j)
  191.         {
  192.             h2 = mapSpec[j].mark;
  193.             HLock ((Handle) h2);
  194.             loop = ConflictCheck (*h1, *h2, "\pformat", "\pformat");
  195.             HUnlock ((Handle) h2);
  196.         }
  197.         if (loop && paraMark[0] > 0)
  198.             loop = ConflictCheck (*h1, paraMark, "\pformat", "\pparagraph");
  199.         if (loop && pageMark[0] > 0)
  200.             loop = ConflictCheck (*h1, pageMark, "\pformat", "\ppage break");
  201.         if (loop && paraMark[0] > 0 && pageMark[0] > 0)
  202.             loop = ConflictCheck (paraMark, pageMark, "\pparagraph", "\ppage break");
  203.         HUnlock ((Handle) h1);
  204.     }
  205. }